package com.sx.zongzhi.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.sx.zongzhi.R;
import com.sx.zongzhi.activity.SelectPersonActivity;
import com.sx.zongzhi.adapter.ContactAdapter;
import com.sx.zongzhi.bean.ContactBean;
import com.sx.zongzhi.presenter.ContactPest;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IContactView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XZY on 2017/11/28.
 * 我的群组
 */
public class MyGroupFragment extends Fragment implements IContactView {


    @BindView(R.id.list_View)
    ListView list_View;
    public List<ContactBean> myGroupList = new ArrayList<>();
    private ContactAdapter contactAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_group,container,false);
        ButterKnife.bind(this,view);
        new ContactPest(this).getContactMyGroupList(getActivity());
        return view;
    }
    @Override
    public void getContactListSuccess(List<ContactBean> contactBeanList) {
        if (contactBeanList != null && contactBeanList.size() > 0 ){
            myGroupList.addAll(contactBeanList);
            List<ContactBean> selectList =((SelectPersonActivity)getActivity()).contactGroupList;
            if (selectList != null){
                for (int i = 0 ; i < myGroupList.size() ; i ++){
                    for (int j = 0 ; j < selectList.size() ; j ++){
                        if (myGroupList.get(i).id.equals(selectList.get(j).id)){
                            myGroupList.get(i).setSelect(true);
                        }
                    }
                }
            }
            contactAdapter = new ContactAdapter(getActivity(),R.layout.item_contact_list,myGroupList);
            list_View.setAdapter(contactAdapter);
        }

    }
    @Override
    public void showError(String message) {
        ToastUtils.showToast(getActivity(),message);
    }

    @Override
    public void onDestroyView() {
        if (contactAdapter != null){
            contactAdapter = null;
        }
        super.onDestroyView();
    }
}
